package repository

import (
	"context"
	"database/sql"
	"gquickstarter/internal/domain"
	"gquickstarter/internal/repository/dao"
)

type IAccountRepository interface {
	Create(ctx context.Context, u domain.Account) error
}

type AccountRepository struct {
	dao dao.IAccountDAO
}

func CreateAccountRepository(dao dao.IAccountDAO) IAccountRepository {
	return &AccountRepository{
		dao: dao}
}

func (a *AccountRepository) Create(ctx context.Context, u domain.Account) error {
	return a.dao.Insert(ctx, a.domainToEntity(u))
}

func (r *AccountRepository) domainToEntity(u domain.Account) dao.Account {
	return dao.Account{
		Id: u.Id,
		Email: sql.NullString{
			String: u.Email,
			// 我确实有手机号
			Valid: u.Email != "",
		},
		Phone: sql.NullString{
			String: u.Phone,
			Valid:  u.Phone != "",
		},
		Password: u.Password,
		//WechatOpenID: sql.NullString{
		//	String: u.WechatInfo.OpenID,
		//	Valid:  u.WechatInfo.OpenID != "",
		//},
		//WechatUnionID: sql.NullString{
		//	String: u.WechatInfo.UnionID,
		//	Valid:  u.WechatInfo.UnionID != "",
		//},
		Ctime: u.Ctime.UnixMilli(),
	}
}
